package com.eastfair.config.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.config.dto.ConfigAttributeValueDTO;
import com.eastfair.config.dto.ConfigAttributeValuePageQuery;
import com.eastfair.config.dto.ConfigAttributeValueSaveDTO;
import com.eastfair.config.dto.ConfigAttributeValueUpdateDTO;
import com.eastfair.config.entity.ConfigAttributeValue;
import com.eastfair.config.service.ConfigAttributeValueService;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import com.eastfair.projectcore.entity.ConfigAttribute;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 *
 * </p>
 *
 * @author zjn
 * @date 2021-08-05
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/configAttributeValue")
@Api(value = "配置模块控制类", tags = "配置模块控制类")
public class ConfigAttributeValueController extends SuperController<ConfigAttributeValueService, Long, ConfigAttributeValue, ConfigAttributeValuePageQuery, ConfigAttributeValueSaveDTO, ConfigAttributeValueUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private ConfigAttributeValueService configAttributeValueService;

    @Override
    public void handlerResult(IPage<ConfigAttributeValue> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ConfigAttributeValue> configAttributeValueList = list.stream().map((map) -> {
            ConfigAttributeValue configAttributeValue = ConfigAttributeValue.builder().build();
            //TODO 请在这里完成转换
            return configAttributeValue;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(configAttributeValueList));
    }

    /**
     * 内部调用 获取配置项实体列表
     *
     * @param <T> 泛型
     * @return
     */
    @PostMapping("/listEntityByClass")
    @ApiOperation(value = "内部调用 获取配置项实体列表", notes = "内部调用 获取配置项实体列表")
    public <T> List<T> listEntityByClass(Class<T> clazz) {
        return configAttributeValueService.listEntityByClass(clazz);
    }

    /**
     * 内部调用 获取配置项实体列表
     *
     * @param <T> 泛型
     * @return
     */
    @PostMapping("/getEntityByClass")
    @ApiOperation(value = "内部调用 获取配置项实体列表", notes = "内部调用 获取配置项实体列表")
    public <T> T getEntityByClass(@RequestBody Class<T> clazz) {
        return configAttributeValueService.getEntityByClass(clazz);
    }

    /**
     * 外部调用 获取配置项实体列表
     *
     * @param configAttribute EntityClass获取实体列表
     * @return
     */
    @PostMapping("/listObjectByEntityClass")
    @ApiOperation(value = "外部调用 获取配置项实体列表", notes = "外部调用 获取配置项实体列表")
    public R<List<Object>> listObjectByEntityClass(@RequestBody ConfigAttribute configAttribute) throws ClassNotFoundException {
        return R.success(configAttributeValueService.listObjectByEntityClass(configAttribute.getEntityClass()));
    }

    /**
     * 插入实体数据
     *
     * @param data 属性值列表，一个对象
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation(value = "插入实体数据", notes = "插入实体数据")
    public R insert(@RequestBody List<ConfigAttributeValueDTO> data) {
        if (data != null && !data.isEmpty()) {
            configAttributeValueService.insertConfiguration(data);
        }
        return R.success();
    }
}